package com.zoctan.api.controller;

import com.zoctan.api.core.response.Result;
import com.zoctan.api.core.response.ResultGenerator;
import com.zoctan.api.entity.Api;
import com.zoctan.api.entity.Dubboapi;
import com.zoctan.api.service.DubboapiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import tk.mybatis.mapper.entity.Condition;

import java.util.List;
import java.util.Map;

/**
 * @author SeasonFan
 * @date 2024/09/26
 */
@RestController
@RequestMapping("/dubboapi")
public class DubboapiController {
    @Resource
    private DubboapiService dubboapiService;

    @PostMapping
    public Result add(@RequestBody Dubboapi dubboapi) {
        dubboapiService.save(dubboapi);
        return ResultGenerator.genOkResult();
    }

    @GetMapping("/apibydeploy")
    public Result listbydeploy(@RequestParam long deployunitid, @RequestParam long modelid) {
        List<Dubboapi> list = dubboapiService.listAllbydeploy(deployunitid, modelid);
        return ResultGenerator.genOkResult(list);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        dubboapiService.deleteById(id);
        return ResultGenerator.genOkResult();
    }

    @PatchMapping
    public Result update(@RequestBody Dubboapi dubboapi) {
        dubboapiService.update(dubboapi);
        return ResultGenerator.genOkResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        Dubboapi dubboapi = dubboapiService.getById(id);
        return ResultGenerator.genOkResult(dubboapi);
    }

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Dubboapi> list = dubboapiService.listAll();
        PageInfo<Dubboapi> pageInfo = PageInfo.of(list);
        return ResultGenerator.genOkResult(pageInfo);
    }

    @PutMapping("/detail")
    public Result updateDeploy(@RequestBody final Dubboapi recipe) {
        dubboapiService.updateDic(recipe);
        return ResultGenerator.genOkResult();
    }

    /**
     * 输入框查询
     */
    @PostMapping("/search")
    public Result search(@RequestBody final Map<String, Object> param) {
        Integer page = Integer.parseInt(param.get("page").toString());
        Integer size = Integer.parseInt(param.get("size").toString());
        PageHelper.startPage(page, size);
        final List<Dubboapi> list = dubboapiService.findDicWithName(param);
        final PageInfo<Dubboapi> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genOkResult(pageInfo);
    }
}
